package com.ssy.lingxi.product.controller.pc.warehouse;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.warehouse.*;
import com.ssy.lingxi.product.api.model.response.warehouse.*;
import com.ssy.lingxi.product.service.FreightSpaceService;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @menu 仓位管理-仓位库存管理
 * @author wrc
 * @date 2020/7/20
 */
@Api(tags = "仓位库存管理")
@RestController
@RequestMapping("/product")
public class FreightSpaceController extends BaseController {
    @Resource
    FreightSpaceService freightSpaceService;

    /**
     * 仓位库存列表查询
     * @param request
     * @return
     */
    @GetMapping("/freight/space/list")
    public Wrapper<PageData<FreightSpaceListResponse>> freightSpaceList(FreightSpaceListRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return freightSpaceService.freightSpaceList(request,sysUser);
    }

    /**
    * 仓位管理-查询全部仓位
    * @author wrc
    * @date 2020/8/7
    */
    @GetMapping("/freight/space/all")
    public Wrapper<List<FreightSpaceListResponse>> freightSpaceAll() {
        return freightSpaceService.freightSpaceAll();
    }

    /**
     * 仓位库存停用&启用
     * @param request
     * @return
     */
    @PostMapping("/freight/space/stop/start")
    public Wrapper freightSpaceStopRoStart(@Validated @RequestBody FreightSpaceStopRoStartRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return freightSpaceService.freightSpaceStopRoStart(request,sysUser);
    }

    /**
     * 仓位库存添加
     * @param request
     * @return
     */
    @PostMapping("/freight/space/add")
    public Wrapper freightSpaceAdd(@Validated @RequestBody FreightSpaceAddRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return freightSpaceService.freightSpaceAdd(request,sysUser);
    }

    /**
     * 仓位库存批量添加
     * @param request
     * @return
     */
    @PostMapping("/freight/space/addBatch")
    public Wrapper freightSpaceAddBatch(@Validated @RequestBody FreightSpaceAddBatchRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return freightSpaceService.freightSpaceAddBatch(request,sysUser);
    }

    /**
     * 仓位库存信息修改
     * @param request
     * @return
     */
    @PostMapping("/freight/space/updata")
    public Wrapper freightSpaceUpdatat(@Validated @RequestBody FreightSpaceUpdataRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return freightSpaceService.freightSpaceUpdatat(request,sysUser);
    }

    /**
     * 仓位库存列表修改库存
     * @param request 参数
     */
    @PostMapping("/freight/space/updateInventory")
    public Wrapper<Boolean> updateInventory(@Valid @RequestBody FreightSpaceUpdateInventoryRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(freightSpaceService.updateInventory(request,sysUser));
    }

    /**
     * 仓位库存信息批量修改
     * @param request 参数
     */
    @PostMapping("/freight/space/updateBatch")
    public Wrapper freightSpaceUpdateBatch(@Validated @RequestBody FreightSpaceUpdateBatchRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return freightSpaceService.freightSpaceUpdateBatch(request,sysUser);
    }

    /**
     * 仓位库存详情
     * @param request
     * @return
     */
    @GetMapping("/freight/space/details")
    public Wrapper<FreightSpaceDetailsResponse> freightSpaceDetails(FreightSpaceDetailsRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return freightSpaceService.freightSpaceDetails(request,sysUser);
    }

    /**
     * 仓位库存调拨-调出
     * @param request
     * @return
     */
    @PostMapping("/freight/space/allot/export")
    public Wrapper<FreightSpaceAllotFoldResponse> freightSpaceAllotExport(@RequestBody FreightSpaceAllotRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return freightSpaceService.freightSpaceAllotExport(request,sysUser);
    }

    /**
     * 仓位库存调拨-调入
     * @param request
     * @return
     */
    @PostMapping("/freight/space/allot/fold")
    public Wrapper<FreightSpaceAllotFoldResponse> freightSpaceAllotFold(@RequestBody FreightSpaceAllotRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return freightSpaceService.freightSpaceAllotFold(request,sysUser);
    }

    /**
     * 仓位库存调拨记录
     * @param request
     * @return
     */
    @GetMapping("/freight/space/allot/fold/log")
    public Wrapper<PageData<FreightSpaceAllotLogListResponse>> freightSpaceAllotFoldLog(FreightSpaceAllotLogListRequest request) {
        return freightSpaceService.freightSpaceAllotFoldLog(request);
    }

    /**
     * 根据仓位ID查询仓位绑定的会员
     * @param request
     * @return
     */
    @GetMapping("/freight/space/mamber/list")
    public Wrapper<PageData<ApplyMemberRequest>> freightSpaceMamberList(FreightSpaceAllotLogListRequest request) {
        return freightSpaceService.freightSpaceMamberList(request);
    }

    /**
     * 根据仓位库存和商品ID查询仓位库存列表
     * @param request
     * @return
     */
    @GetMapping("/freight/space/list/by/productid")
    public Wrapper<List<FreightSpaceProductIdListResponse>> freightSpaceByProductId(FreightSpaceProductIdListRequest request) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return freightSpaceService.freightSpaceByProductId(request,sysUser);
    }

    /**
     * 扣除库存
     * @param request
     * @return
     */
    @PostMapping("/deduct/inventory")
    public Wrapper<Void>  deductInventory(@RequestBody @Valid InventoryByProductIdRequest  request) {
        return freightSpaceService.deductInventory(request);
    }

    /**
     * 返回库存
     * @param request
     * @return
     */
    @PostMapping("/deduct/return")
    public Wrapper<Void>  deductReturn(@RequestBody InventoryByProductIdRequest  request) {
        return freightSpaceService.deductReturn(request);
    }

    /**
     * 根据商品ID查询仓位扣减记录
     * @param request
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/position/deduction/record/list")
    public Wrapper<PageData<PositionInventoryDeductionRecordResponse>>  positionDeductionRecordList(PositionInventoryDeductionRecordRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return freightSpaceService.positionDeductionRecordList(sysUser,request);
    }

    /**
     * 根据商品ID查询仓位扣减记录-平台后台
     * @param request
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/platform/position/deduction/record/list")
    public Wrapper<PageData<PositionInventoryDeductionRecordResponse>>  platformPositionDeductionRecordList(PositionInventoryDeductionRecordRequest  request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return freightSpaceService.platformPositionDeductionRecordList(sysUser,request);
    }

    /**
     *查询商品的仓位库存
     */
    @GetMapping("/stock/byProductId")
    public Wrapper<Double>  stockByProductId(StockByProductIdRequest  request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return freightSpaceService.stockByProductId(request,sysUser);
    }

    /**
     *查询商品的仓位库存-内部
     */
    @GetMapping("/stock/byProductId/inside")
    public Wrapper<Double>  stockByProductIdInside(@RequestParam("productId") Long productId,@RequestParam("shopId")  Long shopId,@RequestParam("memberId")  Long memberId,@RequestParam("memberRoleId") Long memberRoleId) {
        return freightSpaceService.stockByProductIdInside(productId,shopId,memberId,memberRoleId);
    }
}
